package hhm.android.my

import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.databinding.DataBindingUtil
import com.tencent.mm.opensdk.modelmsg.SendAuth
import com.trello.rxlifecycle4.RxLifecycle
import com.trello.rxlifecycle4.android.ActivityEvent
import hhm.android.library.bus.annotation.BusReceiver
import hhm.android.library.utils.OnMultiClickListener
import hhm.android.my.databinding.ActivityAccountAndSafeBinding
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.MainScope
import siau.android.base.*
import siau.android.http.model.WXLoginRequest
import siau.android.library.KeyString

class AccountAndSafeActivity : SBBaseActivity(), CoroutineScope by MainScope() {

    lateinit var dataBinding: ActivityAccountAndSafeBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setTitleWhiteAndTextBlank()
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_account_and_safe)
        initTitle(getString(R.string.account_and_safe))
        dataBinding.lifecycleOwner = this

        val start = registerForActivityResult(IntentIntActivityResultContact()) {
            if (it == 1001) {
                initText()
            }
            if (it == 1024) {
                setResult(it)
                finish()
            }
        }

        dataBinding.activityAccountAndSafePassword.setOnClickListener(object : OnMultiClickListener() {
            override fun onMultiClick(v: View?) {
                if (SBApplication.userData.hasPassword == 1) {
                    start.launch(Intent(this@AccountAndSafeActivity, ChangePasswordActivity::class.java))
                } else {
                    start.launch(Intent(this@AccountAndSafeActivity, SetPasswordActivity::class.java))
                }
            }
        })
        dataBinding.activityAccountAndSafePhoneLl.setOnClickListener(object : OnMultiClickListener() {
            override fun onMultiClick(v: View?) {
                getSureDialog(this@AccountAndSafeActivity, {
                    start.launch(Intent(this@AccountAndSafeActivity, ChangePhoneActivity::class.java))
                }, content = getString(R.string.sure_to_change_phone), showTitle = false)

            }
        })
        dataBinding.activityAccountAndSafeWxLl.setOnClickListener(object : OnMultiClickListener() {
            override fun onMultiClick(v: View?) {
                if (SBApplication.userData.isBindWx == 1) {
                    //去解绑
                    getSureDialog(
                        this@AccountAndSafeActivity, {
                            showLoadingFragment(R.id.activity_account_and_safe_fl)
                            HttpHelper().unBindWx()?.compose(RxLifecycle.bindUntilEvent(lifecycle(), ActivityEvent.STOP))?.subscribe({
                                removeLoadingFragment()
                                SBApplication.userData.isBindWx = 0
                                dataBinding.activityAccountAndSafeWx.text = getString(R.string.unbind)
                            }, {
                                removeLoadingFragment()
                                showToast(it.message)
                            })
                        }, content = getString(R.string.unbind_wx_tip), showTitle = false
                    )
                } else {
                    //去绑定
                    val req = SendAuth.Req()
                    req.scope = "snsapi_userinfo"
                    req.state = "wechat_sdk_demo_test"
                    SBApplication.api.sendReq(req)
                }
            }
        })

        initText()
    }

    private fun initText() {
        if (SBApplication.userData.hasPassword == 1) {
            dataBinding.activityAccountAndSafeTip.text = getString(R.string.change_password)
        } else {
            dataBinding.activityAccountAndSafeTip.text = getString(R.string.set_password_and_safe)
        }
        if (SBApplication.userData.isBindWx == 1) {
            dataBinding.activityAccountAndSafeWx.text = getString(R.string.binded)
        } else {
            dataBinding.activityAccountAndSafeWx.text = getString(R.string.unbind)
        }
        dataBinding.activityAccountAndSafePhone.text = SBApplication.userData.mobile
    }


    @BusReceiver
    fun StringEvent(s: String) {
        if (s.startsWith(KeyString.wxLogin)) {

            val r = WXLoginRequest(s.replace(KeyString.wxLogin, ""))
            HttpHelper().bindWx(r)?.subscribe({
                removeLoadingFragment()
                SBApplication.userData.isBindWx = 1
                dataBinding.activityAccountAndSafeWx.text = getString(R.string.binded)
            }, {
                removeLoadingFragment()
                showToast(it.message)
            })
        }
    }

}